package com.it.dao;


import com.it.base.BaseDAO;
import com.it.bean.Fruit;

import java.util.List;

public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {
    @Override
    public List<Fruit> getFruitList(int page, String queryParem) throws Exception {
        String sql = "select * from t_fruit where fname like ?  or  remark like ? order by fid desc limit ?,5";
        return super.executeQuery(sql, "%" + queryParem + "%", "%" + queryParem + "%", (page - 1) * 5);
    }

    @Override
    public Fruit getFruitByFid(Integer fid) throws Exception {
        String sql = "select * from t_fruit where fid = ?";
        return super.load(sql, fid);
    }

    @Override
    public void updateFruit(Fruit fruit) throws Exception {
        String sql = "update t_fruit set fname = ? , price = ? , fcount = ? , remark = ? where fid = ?";
        super.executeUpdate(sql, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark(), fruit.getFid());

    }

    @Override
    public void delFruit(Integer fid) throws Exception {
        super.executeUpdate("delete from t_fruit where fid = ?", fid);
    }

    @Override
    public void addFruit(Fruit fruit) throws Exception {
        String sql = "insert into t_fruit value (?,?,?,?,?)";
        super.executeUpdate(sql, 0, fruit.getFname(), fruit.getPrice(), fruit.getFcount(), fruit.getRemark());


    }

    @Override
    public int getFruitCount(String queryParem) throws Exception {
        String sql = "select * from t_fruit where fname like ?  or  remark like ?";
        return super.executeQuery(sql, "%" + queryParem + "%", "%" + queryParem + "%").size();
    }
}